﻿using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DemoApplication.Dialog;
using EntityFrameworkLayer.Entities;
using System;
using System.Data;
using System.IO;
using System.Windows;

namespace DemoApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Material m_CurrentSearchMaterial;

        public MainWindow()
        {
            InitializeComponent();
        }

        #region Event handling functions
        
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            MaterialDetailsWindow addDialog = new MaterialDetailsWindow();
            if (true == addDialog.ShowDialog())
            {
                UpdateMaterialGrid();
            }
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            ViewMaterial();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchMaterialWindow searchDialog = new SearchMaterialWindow();
            searchDialog.SearchMaterial = m_CurrentSearchMaterial;
            if (true == searchDialog.ShowDialog())
            {
                m_CurrentSearchMaterial.Copy(searchDialog.SearchMaterial);
                UpdateMaterialGrid();
            }
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            DataSet myDS = new DataSet();
            DataTable table = DataCommunicator.GetMaterialService().Search(m_CurrentSearchMaterial);
            myDS.Tables.Add(table);

            string currentDir = Environment.CurrentDirectory;
            string xmlDataFilePath = Path.Combine(currentDir, "Report", "Material.xml");
            myDS.WriteXml(xmlDataFilePath);

            ReportDocument reportDocument = new ReportDocument();
            string path = Path.Combine(currentDir, "Report", "Material.rpt");
            reportDocument.Load(path);

            // Update the xml file path
            NameValuePairs2 logonProps = new NameValuePairs2();
            logonProps.AddRange(reportDocument.DataSourceConnections[0].LogonProperties);
            foreach (NameValuePair2 pair in logonProps)
            {
                if (pair.Name.ToString().StartsWith("File Path"))
                {
                    pair.Value = xmlDataFilePath;
                    break;
                }
            }
            reportDocument.DataSourceConnections[0].SetLogonProperties(logonProps); 

            Report.Report report = new Report.Report();
            report.crv.ViewerCore.ReportSource = reportDocument;
            report.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            m_CurrentSearchMaterial = new Material();
            UpdateMaterialGrid();
        }

        private void dgvMaterial_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ViewMaterial();
        }

        #endregion

        #region Private functions

        private void UpdateMaterialGrid()
        {
            dgvMaterial.DataContext = DataCommunicator.GetMaterialService().Search(m_CurrentSearchMaterial);
        }

        private int GetMaterialId()
        {
            if (null == dgvMaterial.SelectedItem)
            {
                return -1;
            }

            if (typeof(DataRowView) != dgvMaterial.SelectedItem.GetType())
            {
                return -1;
            }

            DataRowView dataRow = (DataRowView)dgvMaterial.SelectedItem;
            string cellValue = dataRow.Row["Id"].ToString();
            
            int result;
            if (!int.TryParse(cellValue, out result))
            {
                return -1;
            }
            return result;
        }

        private void ViewMaterial()
        {
            int id = GetMaterialId();
            if (-1 == id)
            {
                MessageBox.Show("Please select a material to view");
                return;
            }

            MaterialDetailsWindow openDialog = new MaterialDetailsWindow();
            openDialog.SetMaterialId(id);
            if (true == openDialog.ShowDialog())
            {
                UpdateMaterialGrid();
            }
        }

        #endregion
    }
}
